// let AFRAME = require("./aframe")
// let extras = require("./aframe-extras.min")
function ob(el,src,x,z,maze_size){
    let tile = document.createElement('a-entity');
    el.appendChild(tile);
    tile.setAttribute('position', { x: x, y: 1.25, z: z });
    const material = "obj: #"+src+"-obj; mtl: #"+src+"-mtl"
    tile.setAttribute('obj-model', material);
    tile.setAttribute('scale', '0.5 0.5 0.5');
}
function testob(el,x,z,maze_size,end){
    let tile = document.createElement('a-sphere');

    el.appendChild(tile);
    tile.setAttribute('width', maze_size);
    tile.setAttribute('height', 1.25);
    tile.setAttribute('color', "#EF2D5E");

    tile.setAttribute('depth', maze_size);
    tile.setAttribute('position', { x: x, y: 1.25, z: z });
    tile.setAttribute('static-body', '');
}
// test true练习模式展示具体模型，false实验模式展示球体
export function mazify(){

    AFRAME.registerComponent("mazify", {
        schema: {
            height: {
                default: 20
            },
            width: {
                default: 20
            },
            maze: {
                default: [
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    0,
                    1,
                    1,
                    1,
                    0,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    0,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    1,
                    1,
                    1,
                    0,
                    1,
                    1,
                    1,
                    0,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    1,
                    0,
                    1,
                    1,
                    1,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    1,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    1,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    1,
                    1,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    1,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    1,
                    1,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    1,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    1,
                    1,
                    1,
                    0,
                    0,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    1,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1
            ]        }
        },
        init: function () {
            setTimeout(() => {
                let loader = document.querySelector("#loader");
                loader.remove();
                let scene = document.querySelector("a-scene");
                scene.setAttribute("style", "display:block");
            }, 100)

            // window.addEventListener('keydown', event => {
            //     if (event.code === "Escape") {
            //         let menuDlgElm = document.querySelector("#menuDialog")
            //         menuDlgElm.setAttribute("style", "display: block");
            //     }
            // })
        },

        update: function () {

            // first remove old children if-any
            let oldChildren = this.el.querySelectorAll("a-box");

            if (oldChildren) {
                oldChildren.forEach(element => {
                    this.el.removeChild(element);
                });
            }

            // get data from attributes
            let mazeData = {
                data: this.data.maze,
                height: this.data.height,
                width: this.data.width
            }

            const maze_size = 3;
            const maze_height = 12;
            const el = this.el;

            for (var x = 0; x < mazeData.height; x++) {
                for (var y = 0; y < mazeData.width; y++) {
                    const i = (y * mazeData.width) + x;
                    const position = {
                        x: ((x - (mazeData.width / 2)) * maze_size),
                        y: 1.5,
                        z: (y - (mazeData.height / 2)) * maze_size
                    };
                    if (mazeData.data[i] >= 1 && mazeData.data[i] <= 2) {
                        let wall = document.createElement('a-box');
                        el.appendChild(wall);
                        wall.setAttribute('width', maze_size);
                        wall.setAttribute('height', maze_height);
                        wall.setAttribute('depth', maze_size);
                        wall.setAttribute('position', position);

                        wall.setAttribute('color', '#fff');
                        wall.setAttribute('static-body', '');

                        if (position.x=== -27 && position.z=== -30){
                            wall.setAttribute('material', 'src: #pic1;repeat: 1 1');
                        }else if(position.x=== 0 && position.z=== -24){
                            wall.setAttribute('material', 'src: #pic2;repeat: 1 1');
                        }else if(position.x=== 21 && position.z=== -12){
                            wall.setAttribute('material', 'src: #pic3;repeat: 1 1');
                        }else if(position.x=== 12 && position.z=== 15){
                            wall.setAttribute('material', 'src: #pic4;repeat: 1 1');
                        }else{
                            wall.setAttribute('material', 'src: #brick-01; repeat: 2 4');
                        }
                    }
                }
            }
        }
    })
}
